package com.fjec.base.controller;

import com.alibaba.cloudapi.sdk.constant.SdkConstant;
import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.base.service.ISysLogService;
import com.fjec.common.aliyun.api.IdCardClient;
import com.fjec.common.annotation.OpeLog;
import com.fjec.common.enums.EnumLoginType;
import com.fjec.common.enums.EnumSysCode;
import com.fjec.common.exception.PermissionException;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.shiro.AccountInfo;
import com.fjec.common.shiro.MyUserToken;
import com.fjec.common.util.AliOSSUtil;
import com.fjec.common.util.Base64Util;
import com.fjec.common.util.JsonUtil;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 * 004.日志表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-08-31
 */
@RestController
@RequestMapping("/log")
public class SysLogController {

    @Resource
    private ISysLogService service;

    @Value("shiro-redis.hostAndPort")
    private String hostAndPort;

    @Resource
    private IdCardClient idCardClient;

    @Resource
    private AliOSSUtil storeAliOSSUtil;

    private AtomicInteger i = new AtomicInteger(0);

    @OpeLog(desc = "这是一次测试")
    @GetMapping("/test")
    public String test() throws Exception {
        i.incrementAndGet();
        return String.format("HOST_AND_PROT=[%s], TEST[%s]", hostAndPort, i.get());
    }

    @GetMapping("/test1")
    public String test1() throws Exception {
        i.incrementAndGet();
        Thread.sleep(10 * 1000);
        return String.format("HOST_AND_PROT=[%s], TEST[%s]", hostAndPort, i.get());
    }

    @GetMapping("/t")
    public String t() throws Exception {
        i.incrementAndGet();
        throw new PermissionException(EnumSysCode.ERR_0003);
//        return String.format("HOST_AND_PROT=[%s], TEST[%s]", hostAndPort, i.get());
    }

    @GetMapping("/n/test2")
    public String test2() throws Exception {
        Long userId = AccountInfo.getUserId();
        return String.format("TEST[%s]", userId);
    }

    @GetMapping("/t1")
    public String t1(Page page, String value) throws Exception {
        return value + ":" + page.getCurrent();
    }

    @PostMapping("/t2")
    public String t2(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        File t2file = new File(multipartFile.getName());
        multipartFile.transferTo(t2file);
        String fileName = multipartFile.getOriginalFilename();
        String contentType = multipartFile.getContentType();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Thumbnails.of(t2file).scale(1f).outputQuality(0.25f).toOutputStream(out);
        byte[] bytes = out.toByteArray();
        out.close();
        Map<String, String> map = storeAliOSSUtil.uploadFile(new ByteArrayInputStream(bytes), contentType, fileName);
        Map<String, Object> body = new HashMap<>();
        body.put("image", Base64Util.encode(bytes));
        body.put("configure", "{\"side\":\"face\"}");
        ApiResponse response = idCardClient.syncReq(JsonUtil.toJson(body).getBytes(SdkConstant.CLOUDAPI_ENCODING));
        return new String(response.getBody(), SdkConstant.CLOUDAPI_ENCODING);
    }

    @GetMapping("/login")
    public JsonResult login(String name, String pwd) {
        try {
            MyUserToken userToken = new MyUserToken(name, pwd, EnumLoginType.ACCOUNT_PWD, null);
            Subject subject = SecurityUtils.getSubject();
            subject.login(userToken);
            return JsonResult.success().data(AccountInfo.getSession().getId()).build();
        } catch (Exception ex) {
            return JsonResult.FAILURE;
        }
    }

    @GetMapping("/un_auth")
    public JsonResult un_auth() {
        return JsonResult.FAILURE;
    }

    @GetMapping("/logout")
    public JsonResult logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return JsonResult.SUCCESS;
    }
}
